package com.pirestupppai.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.pirestupppai.R;
import com.pirestupppai.adapter.UserAskAdapter;
import com.pirestupppai.base.BaseAdapter;
import com.pirestupppai.base.BaseFragment;
import com.pirestupppai.base.BaseStatus;
import com.pirestupppai.bean.FeedItem;
import com.pirestupppai.common.Constant;
import com.pirestupppai.network.NetworkRequest;
import com.pirestupppai.network.TupaiSubscriber;
import com.pirestupppai.ui.activity.DetailActivity;
import com.pirestupppai.widgets.view.CustomRecyclerView;
import com.pirestupppai.widgets.view.LoadMoreFooter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author: xiaoluo
 * @date: 2017-04-20 18:12
 */
public class UserAskFragment extends BaseFragment {

    @BindView(R.id.user_ask_recycler_view)
    CustomRecyclerView recyclerView;

    private LoadMoreFooter mFooterView;
    private UserAskAdapter mUserAskAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    private int mUid;
    private int mPosition = 0;  // 列表最后一位
    private int mPage = 1;
    private int mSize = 20;
    private boolean hasMore = true;
    private List<FeedItem> mAskList = new ArrayList<>();

    public UserAskFragment() {
    }

    @SuppressLint("ValidFragment")
    public UserAskFragment(int uid) {
        mUid = uid;
    }

    @Override
    protected Context getFragmentContext() {
        return getActivity();
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.fragment_user_ask;
    }

    @Override
    protected void initViews() {
        mFooterView = new LoadMoreFooter(mContext);
        mUserAskAdapter = new UserAskAdapter(mContext, mAskList);
        mUserAskAdapter.addFooterView(mFooterView);
        mLinearLayoutManager = new LinearLayoutManager(mContext);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setEmptyText("暂时没有任何求P哟");
        recyclerView.getRecyclerView().setLayoutManager(mLinearLayoutManager);
        recyclerView.getRecyclerView().setAdapter(mUserAskAdapter);
        recyclerView.getRecyclerView().setHasFixedSize(true);
    }

    @Override
    protected void initListeners() {
        // 监听到达底部事件
        recyclerView.getRecyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //到达底部时,加载更多
                int lastVisiblePosition = mLinearLayoutManager.findLastCompletelyVisibleItemPosition() + 1;
                if (mLinearLayoutManager.getChildCount() > 0
                        && mLinearLayoutManager.getItemCount() >= mLinearLayoutManager.getChildCount()
                        && lastVisiblePosition >= mLinearLayoutManager.getItemCount()
                        && dy > 0) {
                    mPosition = lastVisiblePosition;

                    if (hasMore) {
                        mFooterView.setStatus(LoadMoreFooter.Status.LOADING);
                        getData(BaseStatus.LOADMORE);
                    }
                }
            }
        });

        mUserAskAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Intent intent = new Intent();
                intent.putExtra(Constant.IntentKey.ID, mAskList.get(position).getId());
                intent.setClass(mContext, DetailActivity.class);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    protected void initLogic() {
        getData(BaseStatus.REFRESH);
    }

    /**
     * 获取数据
     */
    private void getData(final BaseStatus status) {
        if (status == BaseStatus.REFRESH) {
            mFooterView.setStatus(LoadMoreFooter.Status.GONE);
            hasMore = true;
            mPage = 1;
        } else {
            mPage++;
        }

        NetworkRequest.getInstance().getUserAsk(mUid, mPage, mSize, mContext,
                new TupaiSubscriber<List<FeedItem>>(mContext) {
                    @Override
                    protected void requestSuccess(List<FeedItem> items) {
                        if (items != null && items.size() != 0) {
                            if (status == BaseStatus.REFRESH) {
                                mAskList.clear();
                                mAskList.addAll(items);
                                mUserAskAdapter.notifyDataSetChanged();
                            } else {
                                mAskList.addAll(items);
                                mUserAskAdapter.notifyItemInserted(mPosition);
                            }

                        } else if (status == BaseStatus.REFRESH) {
                            recyclerView.setEmpty();
                        } else {
                            mFooterView.setStatus(LoadMoreFooter.Status.THE_END);
                        }

                        if (items != null && items.size() < mSize) {
                            mFooterView.setStatus(LoadMoreFooter.Status.THE_END);
                            hasMore = false;
                        }
                    }

                    @Override
                    protected void requestError(String error) {
                        if (status == BaseStatus.LOADMORE) {
                            mFooterView.setStatus(LoadMoreFooter.Status.GONE);
                            mPage--;
                        }
                    }
                });
    }
}
